import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:top_foundation/utils/theme/theme.dart';
import 'package:whale/helper/utils/popup/wheel.dart';
import 'package:whale/helper/utils/util.dart';

import 'index.dart';

abstract class PopupBottomBaseView extends StatefulWidget {
  const PopupBottomBaseView({
    super.key,
    this.backgroundColor,
    this.backgroundImage,
    this.height,
    this.cancelText,
    this.cancelColor,
    this.sureColor,
    this.sureText,
    this.initIndex,
    this.unSelectedStyle,
    this.selectedStyle,
    this.title,
  });
  final Color? backgroundColor;
  final ImageProvider<Object>? backgroundImage;
  final double? height;
  final String? cancelText;
  final String? sureText;
  final Color? cancelColor;
  final Color? sureColor;
  final int? initIndex;
  final TextStyle? unSelectedStyle;
  final TextStyle? selectedStyle;
  final String? title;
}

abstract class PopupBottomBaseViewState<T extends PopupBottomBaseView>
    extends State<T> {
  @protected
  Widget buildChild(BuildContext context) => const SizedBox();

  @protected
  void onSure();

  late TextStyle unSelectedStyle;
  late TextStyle selectedStyle;

  void _setupStyle() {
    unSelectedStyle = widget.unSelectedStyle ?? Popup.unSelectedStyle;
    selectedStyle = widget.selectedStyle ?? Popup.selectedStyle;
  }

  @override
  Widget build(BuildContext context) {
    _setupStyle();
    return SafeArea(
      child: Container(
        constraints: BoxConstraints(maxHeight: 0.6.sh),
        decoration: BoxDecoration(
            color: widget.backgroundColor,
            image: widget.backgroundImage == null
                ? null
                : DecorationImage(image: widget.backgroundImage!)),
        child: Column(
          children: [
            if (widget.sureText != null ||
                widget.cancelText != null ||
                widget.title != null)
              AppBar(
                title: Text(
                  widget.title ?? '',
                  style: ITheme.text.titleLarge,
                ),
                leadingWidth: 50.r,
                leading: TextButton(
                    onPressed: () => Get.back(result: false),
                    child: Text(
                      widget.cancelText!,
                      style: TextStyle(
                          color: ITheme.contentColor, fontSize: 13.sp),
                    )),
                actions: [
                  if (widget.sureText != null)
                    TextButton(
                        onPressed: onSure,
                        style: ButtonStyle(
                            fixedSize:
                                MaterialStatePropertyAll(Size.fromWidth(50.r))),
                        child: Text(
                          widget.sureText!,
                          style: TextStyle(
                              color: ITheme.selectedColor, fontSize: 13.sp),
                        )),
                ],
                backgroundColor: Colors.transparent,
              ),
            Expanded(child: buildChild(context))
          ],
        ),
      ),
    );
  }
}

class PopupBottomListView extends PopupBottomBaseView {
  const PopupBottomListView({
    super.key,
    super.height,
    super.backgroundColor,
    super.backgroundImage,
    super.cancelColor,
    super.cancelText,
    super.sureColor,
    super.sureText,
    super.initIndex,
    super.unSelectedStyle,
    super.selectedStyle,
    required this.data,
    required this.onTap,
    this.itemHeight,
  });
  final List<String> data;
  final void Function(int) onTap;
  final double? itemHeight;

  @override
  State<PopupBottomListView> createState() => _PopupBottomListViewState();
}

class _PopupBottomListViewState
    extends PopupBottomBaseViewState<PopupBottomListView> {
  @override
  void onSure() {}

  @override
  Widget buildChild(BuildContext context) {
    return SingleChildScrollView(
      physics: const ClampingScrollPhysics(),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: widget.data
            .asMap()
            .map((key, value) => MapEntry(
                key,
                GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    Navigator.of(context).pop();
                    widget.onTap(key);
                  },
                  child: Container(
                    height: widget.itemHeight ?? Popup.itemHeight,
                    alignment: Alignment.center,
                    child: Text(
                      value,
                      style: key == widget.initIndex
                          ? selectedStyle
                          : unSelectedStyle,
                    ),
                  ),
                )))
            .values
            .toList(),
      ),
    );
  }
}

class PopupBottomRadioListView extends PopupBottomBaseView {
  const PopupBottomRadioListView({
    super.key,
    super.height,
    super.backgroundColor,
    super.backgroundImage,
    super.cancelColor,
    super.cancelText,
    super.sureColor,
    super.sureText,
    this.initIndexs,
    super.title,
    required this.data,
    this.isSingleSelect = true,
    this.itemHeight,
    required this.onSure,
  });
  final List<int>? initIndexs;
  final List<String> data;
  final bool isSingleSelect;
  final double? itemHeight;
  final ValueChanged<List<int>> onSure;
  @override
  State<PopupBottomRadioListView> createState() =>
      _PopupBottomRadioListViewState();
}

class _PopupBottomRadioListViewState
    extends PopupBottomBaseViewState<PopupBottomRadioListView> {
  late List<int> selectedIndexs;

  @override
  void onSure() {
    widget.onSure(selectedIndexs);
    Get.back(result: selectedIndexs);
  }

  @override
  void initState() {
    selectedIndexs = widget.initIndexs ?? [];
    super.initState();
  }

  @override
  void didUpdateWidget(covariant PopupBottomRadioListView oldWidget) {
    selectedIndexs = widget.initIndexs ?? [];
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget buildChild(BuildContext context) {
    return ListView.builder(
      physics: const ClampingScrollPhysics(),
      itemBuilder: (context, index) {
        var m = widget.data[index];
        var isSelected = selectedIndexs.contains(index);
        return InkWell(
          onTap: () {
            if (widget.isSingleSelect) {
              if (selectedIndexs.contains(index)) {
                selectedIndexs.remove(index);
              } else {
                selectedIndexs = [index];
              }
            } else {
              if (selectedIndexs.contains(index)) {
                selectedIndexs.remove(index);
              } else {
                selectedIndexs.add(index);
              }
            }
            setState(() {});
          },
          child: Container(
            height: widget.itemHeight ?? Popup.itemHeight,
            decoration: BoxDecoration(
                border: Border(bottom: BorderSide(color: ITheme.dividerColor))),
            padding: EdgeInsets.symmetric(horizontal: 16.r),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  m,
                  style: isSelected ? selectedStyle : unSelectedStyle,
                ),
                Icon(
                  isSelected
                      ? Icons.check_circle_rounded
                      : Icons.circle_outlined,
                  size: 25.r,
                  color:
                      isSelected ? selectedStyle.color : unSelectedStyle.color,
                )
              ],
            ),
          ),
        );
      },
      itemCount: widget.data.length,
    );
  }
}

class PopupBottomWheelView extends PopupBottomBaseView {
  const PopupBottomWheelView({
    super.key,
    super.height,
    super.backgroundColor,
    super.backgroundImage,
    super.cancelColor,
    super.cancelText,
    super.sureColor,
    super.sureText,
    super.initIndex,
    super.unSelectedStyle,
    super.selectedStyle,
    required this.data,
    this.onChange,
    this.onSure,
  });

  final List<String> data;
  final ValueChanged<int>? onChange;
  final ValueChanged<int>? onSure;
  @override
  State<PopupBottomWheelView> createState() => _PopupBottomWheelViewState();
}

class _PopupBottomWheelViewState
    extends PopupBottomBaseViewState<PopupBottomWheelView> {
  int curr = 0;

  @override
  void onSure() {
    if (widget.onSure != null) {
      widget.onSure!(curr);
    }
  }

  @override
  void initState() {
    curr = widget.initIndex ?? 0;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant PopupBottomWheelView oldWidget) {
    curr = widget.initIndex ?? 0;
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget buildChild(BuildContext context) {
    return WheelListView(
      list: widget.data,
      value: widget.data[curr],
      valueChanged: (value) {
        curr = widget.data.indexOf(value);
        if (widget.onChange != null) {
          widget.onChange!(curr);
        }
      },
    );
  }
}
